package com.ruoyi.web.controller.members;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.constant.UnionConstants;
import com.ruoyi.common.enums.UnionsStatus;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.members.domain.SysMembers;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.members.domain.SysGrassrootsTradeUnions ;
import com.ruoyi.members.service.ISysGrassrootsTradeUnionsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 基层工会Controller
 * 
 * @author ruoyi
 * @date 2025-09-24
 */
@RestController
@RequestMapping("/system/unions")
public class SysGrassrootsTradeUnionsController extends BaseController
{
    @Autowired
    private ISysGrassrootsTradeUnionsService sysGrassrootsTradeUnionsService;

    /**
     * 组织工作 - 基层工会管理 - 查询列表
     */
    @PreAuthorize("@ss.hasPermi('system:unions:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysGrassrootsTradeUnions sysGrassrootsTradeUnions)
    {
        //MybatisPlus - 条件分页查询
        Page<SysGrassrootsTradeUnions> objectPage = new Page<>(sysGrassrootsTradeUnions.getPageNum(), sysGrassrootsTradeUnions.getPageSize());
        LambdaQueryWrapper<SysGrassrootsTradeUnions> sysGrassrootsTradeUnionsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //根据基层工会名称查询
        if (sysGrassrootsTradeUnions.getUnionsName() != null){
            sysGrassrootsTradeUnionsLambdaQueryWrapper.like(SysGrassrootsTradeUnions::getUnionsName, sysGrassrootsTradeUnions.getUnionsName());
        }
        Page<SysGrassrootsTradeUnions> page = sysGrassrootsTradeUnionsService.page(objectPage, sysGrassrootsTradeUnionsLambdaQueryWrapper);
        return new TableDataInfo(page.getRecords(), page.getTotal(), page.getCurrent(), page.getSize());
    }

    /**
     * 组织工作 - 基层工会管理 - 导出基层工会列表
     */
    @PreAuthorize("@ss.hasPermi('system:unions:export')")
    @Log(title = "基层工会", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysGrassrootsTradeUnions sysGrassrootsTradeUnions)
    {
        //MybatisPlus - 条件分页查询
        LambdaQueryWrapper<SysGrassrootsTradeUnions> sysGrassrootsTradeUnionsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //根据基层工会名称查询
        if (sysGrassrootsTradeUnions.getUnionsName() != null){
            sysGrassrootsTradeUnionsLambdaQueryWrapper.like(SysGrassrootsTradeUnions::getUnionsName, sysGrassrootsTradeUnions.getUnionsName());
        }

        List<SysGrassrootsTradeUnions> list = sysGrassrootsTradeUnionsService.list(sysGrassrootsTradeUnionsLambdaQueryWrapper);
        ExcelUtil<SysGrassrootsTradeUnions> util = new ExcelUtil<SysGrassrootsTradeUnions>(SysGrassrootsTradeUnions.class);
        util.exportExcel(response, list, "基层工会数据","基层工会信息统计表");
    }

    /**
     * 组织工作 - 基层工会管理 - 获取基层工会详细信息（ID）
     */
    @PreAuthorize("@ss.hasPermi('system:unions:query')")
    @GetMapping(value = "/{unionsId}")
    public AjaxResult getInfo(@PathVariable("unionsId") Long unionsId)
    {
        return success(sysGrassrootsTradeUnionsService.getById(unionsId));
    }

    /**
     * 组织工作 - 基层工会管理 - 新增基层工会
     */
    @PreAuthorize("@ss.hasPermi('system:unions:add')")
    @Log(title = "基层工会", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysGrassrootsTradeUnions sysGrassrootsTradeUnions)
    {
        //判断基层工会名称是否存在
        LambdaQueryWrapper<SysGrassrootsTradeUnions> sysGrassrootsTradeUnionsLambdaQueryWrapper = new LambdaQueryWrapper<>(SysGrassrootsTradeUnions.class)
                .eq(SysGrassrootsTradeUnions::getUnionsName, sysGrassrootsTradeUnions.getUnionsName());
        List<SysGrassrootsTradeUnions> list = sysGrassrootsTradeUnionsService.list(sysGrassrootsTradeUnionsLambdaQueryWrapper);
        if (!list.isEmpty()){
            return error(UnionsStatus.UNIONS_EXIST.getInfo());
        }
        sysGrassrootsTradeUnions.setCreateBy(SecurityUtils.getUsername());
        return toAjax(sysGrassrootsTradeUnionsService.save(sysGrassrootsTradeUnions));
    }

    /**
     * 组织工作 - 基层工会管理 - 修改基层工会
     */
    @PreAuthorize("@ss.hasPermi('system:unions:edit')")
    @Log(title = "基层工会", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysGrassrootsTradeUnions sysGrassrootsTradeUnions)
    {
        //判断基层工会是否存在
        SysGrassrootsTradeUnions byId = sysGrassrootsTradeUnionsService.getById(sysGrassrootsTradeUnions.getUnionsId());
        if (byId == null){
            return error(UnionsStatus.UNIONS_NOT_EXIST.getInfo());
        }
        sysGrassrootsTradeUnions.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(sysGrassrootsTradeUnionsService.updateById(sysGrassrootsTradeUnions));
    }

    /**
     * 组织工作 - 基层工会管理 - 删除基层工会
     */
    @PreAuthorize("@ss.hasPermi('system:unions:remove')")
    @Log(title = "基层工会", businessType = BusinessType.DELETE)
	@DeleteMapping("/{unionsIds}")
    public AjaxResult remove(@PathVariable Long[] unionsIds)
    {
        //判断基层工会是否存在
        List<SysGrassrootsTradeUnions> list = sysGrassrootsTradeUnionsService.listByIds(Arrays.asList(unionsIds));
        if (list.isEmpty()){
            return error(UnionsStatus.UNIONS_NOT_EXIST.getInfo());
        }
        return toAjax(sysGrassrootsTradeUnionsService.removeByIds(Arrays.asList(unionsIds)));
    }
}
